package com.totoro.tymapping.task;

import com.totoro.tymapping.TyMapping;
import com.totoro.tymapping.bo.CheckParseBo;
import com.totoro.tymapping.data.ComputeResult;
import com.totoro.tymapping.enums.ComputeExecutorEnum;
import com.totoro.tymapping.executor.compute.ComputeExecutor;
import com.totoro.tymapping.utils.PotionEffectUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/13 - 16:32
 * @description
 **/
public class AttrPotionEffectTask extends BukkitRunnable {

    private final TyMapping plugin;

    public AttrPotionEffectTask(TyMapping plugin) {
        this.plugin = plugin;
    }

    @Override
    public void run() {
        Bukkit.getScheduler().runTask(plugin, () -> {
            Map<UUID, CheckParseBo> map = plugin.getCheckTaskManager().getPlayerCheckParseBoMap();
            map.forEach((uuid, checkParseBo) -> {
                Player player = Bukkit.getPlayer(uuid);
                if(null == player || !player.isOnline()) {
                    plugin.getCheckTaskManager().remove(uuid);
                    return;
                }
                if(null == checkParseBo) {
                    return;
                }
                if(StringUtils.isBlank(checkParseBo.getCheckId())) {
                    return;
                }
                // 添加药水效果
                List<String> effect = checkParseBo.getEffect();
                effect.forEach(e -> {
                    String[] effectSplit = e.split("-");
                    String potionName = effectSplit[0];
                    PotionEffectType potionEffectType = PotionEffectUtil.getPotionEffectByName(potionName);
                    if(null == potionEffectType) {
                        return;
                    }
                    int potionLevel = 1;
                    try {
                        potionLevel = Integer.parseInt(effectSplit[1]);
                    } catch (NumberFormatException exception) {
                        return;
                    }
                    // 创建不显示粒子和图标的药水效果
                    PotionEffect hiddenEffect = new PotionEffect(potionEffectType, 40, potionLevel, true, false);
                    player.addPotionEffect(hiddenEffect);
                });

                // 添加属性
                // TODO

            });
        });
    }
}
